package com.xique.park.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.dto.ParkDeviceStatisticsDto;
import com.xique.park.bean.request.web.ipc.ParkIpcListRequest;
import com.xique.park.mapper.ParkDeviceNewMapper;
import com.xique.park.service.service.IParkDeviceNewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备--newService业务层处理
 *
 * @author xique
 * @date 2021-12-09
 */
@Service
public class ParkDeviceNewServiceImpl extends ServiceImpl<ParkDeviceNewMapper, ParkDeviceNew> implements IParkDeviceNewService {

    @Resource
    private ParkDeviceNewMapper parkDeviceNewMapper;

    /**
     * 查询设备--new
     *
     * @param id 设备--newID
     * @return 设备--new
     */
    @Override
    public ParkDeviceNew selectParkDeviceNewById(Long id) {
        return parkDeviceNewMapper.selectById(id);
    }

    @Override
    public ParkDeviceNew selectParkDeviceNewByCode(String code) {
        LambdaQueryWrapper<ParkDeviceNew> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ParkDeviceNew::getCode, code);
        queryWrapper.eq(ParkDeviceNew::getRecordStatus, Constants.NORMAL);
        return getOne(queryWrapper);
    }

    /**
     * 查询设备--new列表
     *
     * @param parkDeviceNew 设备--new
     * @return 设备--new
     */
    @Override
    public List<ParkDeviceNew> selectParkDeviceNewList(ParkDeviceNew parkDeviceNew) {
        QueryWrapper<ParkDeviceNew> wrapper = new QueryWrapper<>();
        wrapper.eq(null != parkDeviceNew.getParkId(), "park_id", parkDeviceNew.getParkId());
        wrapper.eq(null != parkDeviceNew.getLaneId(), "lane_id", parkDeviceNew.getLaneId());
        wrapper.eq(StringUtils.isNotEmpty(parkDeviceNew.getIsMain()), "is_main", parkDeviceNew.getIsMain());
        wrapper.like(StringUtils.isNotEmpty(parkDeviceNew.getCode()), "code", parkDeviceNew.getCode());
        wrapper.eq(StringUtils.isNotEmpty(parkDeviceNew.getStatus()), "status", parkDeviceNew.getStatus());
        return parkDeviceNewMapper.selectList(wrapper);
    }

    @Override
    public IPage<ParkDeviceNew> selectParkIpcPage(ParkIpcListRequest search) {
        Page<ParkDeviceNew> page = new Page<>();
        page.setCurrent(search.getPageNum());
        page.setSize(search.getPageSize());
        return parkDeviceNewMapper.selectParkIpcPage(page, search);
    }

    /**
     * 新增设备--new
     *
     * @param parkDeviceNew 设备--new
     * @return 结果
     */
    @Override
    public int insertParkDeviceNew(ParkDeviceNew parkDeviceNew) {
        parkDeviceNew.setCreateTime(DateUtils.getNowDate());
        return parkDeviceNewMapper.insert(parkDeviceNew);
    }

    /**
     * 修改设备--new
     *
     * @param parkDeviceNew 设备--new
     * @return 结果
     */
    @Override
    public int updateParkDeviceNew(ParkDeviceNew parkDeviceNew) {
        parkDeviceNew.setUpdateTime(DateUtils.getNowDate());
        return parkDeviceNewMapper.updateById(parkDeviceNew);
    }

    /**
     * 批量删除设备--new
     *
     * @param ids 需要删除的设备--newID
     * @return 结果
     */
    @Override
    public int deleteParkDeviceNewByIds(List<Long> ids) {
        return parkDeviceNewMapper.deleteBatchIds(ids);
    }

    /**
     * 删除设备--new信息
     *
     * @param id 设备--newID
     * @return 结果
     */
    @Override
    public int deleteParkDeviceNewById(Long id) {
        return parkDeviceNewMapper.deleteById(id);
    }

    @Override
    public ParkDeviceStatisticsDto selectOnLineStatus(Long parkId) {
        return selectOnLineStatus(null, null, null, null, parkId);
    }

    @Override
    public ParkDeviceStatisticsDto selectOnLineStatus(Integer province, Integer city, Integer district, Long deptId, Long parkInfoId) {
        List<ParkDeviceStatisticsDto> parkDeviceStatisticsDtoList = parkDeviceNewMapper.selectOnLineStatus(province, city, district, deptId, parkInfoId);
        int totalNum = parkDeviceStatisticsDtoList.size();
        int onlineNum = 0;
        int noOnlineNum = 0;
        if (CollUtil.isNotEmpty(parkDeviceStatisticsDtoList)) {
            for (ParkDeviceStatisticsDto parkDeviceStatisticsDto : parkDeviceStatisticsDtoList) {
                if (null != parkDeviceStatisticsDto) {
                    String onlineStatusJson = parkDeviceStatisticsDto.getOnlineStatus();
                    if (StrUtil.isNotBlank(onlineStatusJson)) {
                        JSONObject onlineStatusObj = JSON.parseObject(onlineStatusJson);
                        String state = onlineStatusObj.getString("state");
                        if (StrUtil.equals("offline", state)) {
                            noOnlineNum++;
                        } else if (StrUtil.equals("online", state)) {
                            onlineNum++;
                        }
                    } else {
                        noOnlineNum++;
                    }
                }
            }
        }
        return ParkDeviceStatisticsDto.builder().totalNum(totalNum).onlineNum(onlineNum).noOnlineNum(noOnlineNum).build();
    }

}
